local sk__canlve = fk.CreateSkill {

  name = "sk__canlve",

  tags = {  },

}



sk__canlve:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__canlve.name) then
      for _, move in ipairs(data) do
        if move.from and move.to and move.from ~= move.to and
          (move.from == player or move.to == player) and
          (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip) then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if move.to == player and not player.room:getPlayerById(move.from.id).dead then
                return true
              end
              if move.from == player and not player.room:getPlayerById(move.to.id).dead and
                not player.room:getPlayerById(move.to.id):isNude() then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets, dat1, dat2 = {}, {}, {}
    for _, move in ipairs(data) do
      if move.from and move.to and move.from ~= move.to and
        (move.from == player or move.to == player) and
        (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if move.to == player then
              table.insertIfNeed(targets, move.from)
              table.insertIfNeed(dat1, move.from)
            end
            if move.from == player then
              table.insertIfNeed(targets, move.to)
              table.insertIfNeed(dat2, move.to)
            end
          end
        end
      end
    end
    room:sortByAction(targets)
    for _, id in ipairs(targets) do
      if not player:hasSkill(sk__canlve.name) then break end
      local to = id
      if not to.dead then
        if table.contains(dat1, id) then
          self:doCost(event, to, player, 1)
        end
        if not player:hasSkill(sk__canlve.name) then break end
        if table.contains(dat2, id) and not to.dead and not to:isNude() then
          self:doCost(event, to, player, 2)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__canlve.name, nil, "#sk__canlve"..data.."-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__canlve.name)
    room:doIndicate(player.id, {target.id})
    if data == 1 then
      room:notifySkillInvoked(player, sk__canlve.name, "offensive")
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = sk__canlve.name,
      }
    else
      room:notifySkillInvoked(player, sk__canlve.name, "control")
      room:askForDiscard(target, 1, 1, true, sk__canlve.name, false)
    end
  end,
})

return sk__canlve